08. Variables 1
Variables 1
Start Quiz:
# Mary is a world class spy with different aliases across the world.
# Mary is known as Randa in America.
# But in Europe, her alias Randa has another alias known as Katie.
# In Asia, the alias Katie has another alias known as Salwa.
# In Australia, the alias Salwa is known as Kathleen.
# In South America, the alias Kathleen is known as the alias Gabriel.
# You're a spy yourself and you want to be able to print the real name associated with
# all of these alias names to keep track of Mary, but you only know that
# gabriel = kathleen, and kathleen = salwa, etc..
# Your mission: knowing how each alias relates to each other, assign the variables
# gabriel, kathleen, salwa, katie, and randa to each other so whenever we print any alias,
# the values for each alias will point to the string "Mary"
# NOTE: We can't simply assign all variables to "Mary".
mary = "Mary"
# Your code goes here
# In South America, the alias Kathleen is known as the alias Gabriel, this means that:
gabriel = kathleen
# Test to see if all of these variables will print out the string "Mary"
print gabriel
print kathleen
print salwa
print katie
print randa
print mary
Solution:
Breaking Down the Problem
We first need to recognize what the problem is asking. The problem is asking us to assign different variables in order to be able have all variables print the string "Mary." We cannot simply assign all variables to the string "Mary".
What To Do
Therefore we need to follow the relationship that each alias has with each other and assign variables to each other. Python will be able to figure trace the full reference of each variable and figure out the string that the variable is pointing to.
Answer Code
Here is the relationship between all of these aliases:
Mary is known as Randa in America.
But in Europe, her alias Randa has another alias known as Katie.
In Asia, the alias Katie has another alias known as Salwa.
In Australia, the alias Salwa is known as Kathleen.
In South America, the alias Kathleen is known as the alias Gabriel.
Let's translate this into something more straight forward:
- randa -> mary
- katie -> randa
- salwa -> katie
- kathleen -> salwa
- gabriel -> kathleen
Therefore we can write our variable assignment code:
randa = mary
katie = randa
salwa = katie
kathleen = salwa
gabriel = kathleen